package com.dbuil.bleshoes.ui.view;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.dbuil.bleshoes.R;

public class CircleProgressView extends View {

    /**
     * 画笔
     */
    private Paint paint;
    /**
     * 宽度
     */
    private float width;
    /**
     * 高度
     */
    private float height;
    /**
     * 中心坐标
     */
    private float circleX, circleY;
    /**
     * 半径
     */
    private float progressRadius = dpToPx(45);
    /**
     * 进度背景颜色
     */
    private int progressBackgroundColor = Color.parseColor("#EDEDED");
    /**
     * 进度颜色
     */
    private int progressColor = Color.parseColor("#358AFB");
    /**
     * 进度
     */
    private float progress = 0F;
    /**
     * 进度宽度
     */
    private float progressStrokeWidth = dpToPx(5);
    /**
     * 进度开始角度
     */
    private float progressStartAngle = -90;


    public CircleProgressView(Context context) {
        super(context);
        init(context, null);
    }

    public CircleProgressView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public CircleProgressView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.CircleProgressView);
        progress = array.getFloat(R.styleable.CircleProgressView_progress, 0F);
        progressColor = array.getColor(R.styleable.CircleProgressView_progress_color, progressColor);
        progressBackgroundColor = array.getColor(R.styleable.CircleProgressView_progress_background_color, progressBackgroundColor);
        progressStartAngle = array.getFloat(R.styleable.CircleProgressView_progress_start_angle, progressStartAngle);
        progressStrokeWidth = array.getDimension(R.styleable.CircleProgressView_progress_stroke_width, progressStrokeWidth);
        progressRadius = array.getDimension(R.styleable.CircleProgressView_progress_radius, progressRadius);

        paint = new Paint();
        paint.setStyle(Paint.Style.STROKE);//只描边，不填充
        paint.setAntiAlias(true);
        paint.setStrokeWidth(progressStrokeWidth);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width = getMeasuredWidth();
        height = getMeasuredHeight();
        circleX = width / 2;
        circleY = height / 2;
        progressRadius -= getPaddingLeft() - getPaddingRight();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        paint.setColor(progressBackgroundColor);
        canvas.drawCircle(circleX, circleY, progressRadius, paint);
        paint.setColor(progressColor);
        RectF rectF = new RectF(circleX - progressRadius, circleY - progressRadius, circleX + progressRadius, circleY + progressRadius);
        canvas.drawArc(rectF, progressStartAngle, 360 * progress / 100, false, paint);
    }


    /**
     * 获取屏幕密度
     *
     * @return
     */
    private static float getScreenDensity() {
        return Resources.getSystem().getDisplayMetrics().density;
    }


    /**
     * dp to px
     *
     * @param dp
     * @return
     */
    private static float dpToPx(float dp) {
        return dp * getScreenDensity();
    }


    public void setWidth(float width) {
        this.width = width;
    }

    public void setHeight(float height) {
        this.height = height;
    }

    public float getCircleX() {
        return circleX;
    }

    public void setCircleX(float circleX) {
        this.circleX = circleX;
    }

    public float getCircleY() {
        return circleY;
    }

    public void setCircleY(float circleY) {
        this.circleY = circleY;
    }

    public float getProgressRadius() {
        return progressRadius;
    }

    public void setProgressRadius(float progressRadius) {
        this.progressRadius = progressRadius;
    }

    public float getProgressStartAngle() {
        return progressStartAngle;
    }

    public void setProgressStartAngle(float progressStartAngle) {
        this.progressStartAngle = progressStartAngle;
    }

    public int getProgressBackgroundColor() {
        return progressBackgroundColor;
    }

    public void setProgressBackgroundColor(int progressBackgroundColor) {
        this.progressBackgroundColor = progressBackgroundColor;
    }

    public int getProgressColor() {
        return progressColor;
    }

    public void setProgressColor(int progressColor) {
        this.progressColor = progressColor;
    }

    public float getProgress() {
        return progress;
    }

    public void setProgress(float progress) {
        this.progress = progress;
    }

    public float getProgressStrokeWidth() {
        return progressStrokeWidth;
    }

    public void setProgressStrokeWidth(float progressStrokeWidth) {
        this.progressStrokeWidth = progressStrokeWidth;
    }

}